﻿/*
Copyright © 2010 SectorPoint, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
using System;
using System.ComponentModel;

namespace SectorPoint.Net.Mail
{
	/// <summary>
	/// Post Office Protocol v3 commands (see RFC 1939).
	/// </summary>
	[TypeConverter( typeof( Pop3CommandConverter ) )]
	internal enum Pop3Command
	{
		/// <summary>
		/// An alternate command for authentication which uses an MD5 digest for the password.
		/// </summary>
		Apop,
		/// <summary>
		/// Marks the specified message for deletion.
		/// </summary>
		Delete,
		/// <summary>
		/// Returns a scan listing of all messages in a maildrop, or if an argument was supplied, returns a scan listing for the specified message.
		/// </summary>
		List,
		/// <summary>
		/// The POP3 server does nothing, it merely replies with a positive response.
		/// </summary>
		NoOperation,
		/// <summary>
		/// Sends the user's password.
		/// </summary>
		Password,
		/// <summary>
		/// If any messages have been marked as deleted by the POP3 server, they are unmarked.  The POP3 server then replies with a positive response.
		/// </summary>
		Reset,
		/// <summary>
		/// Retrieves the specified message from the maildrop.
		/// </summary>
		Retrieve,
		/// <summary>
		/// The POP3 server issues a positive response with a line containing information for the maildrop.  This line is called a "drop listing" for that maildrop.
		/// </summary>
		Status,
		/// <summary>
		/// Sends the username.
		/// </summary>
		User,
		/// <summary>
		/// The POP3 server removes all messages marked as deleted from the maildrop and replies as to the status of this operation.
		/// </summary>
		Quit
	}

	/// <summary>
	/// Used to convert Pop3Command enumeration friendly names into the actual command. Only converts into the System.String type.
	/// </summary>
	internal class Pop3CommandConverter : EnumConverter
	{
		public Pop3CommandConverter()
			: base( typeof( Pop3Command ) )
		{
		}

		public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType )
		{
			if (destinationType == typeof( string ))
			{
				switch ((Pop3Command)value)
				{
					case Pop3Command.Apop:
						return "APOP";
						
					case Pop3Command.Delete:
						return "DELE";

					case Pop3Command.List:
						return "LIST";

					case Pop3Command.NoOperation:
						return "NOOP";

					case Pop3Command.Password:
						return "PASS";

					case Pop3Command.Reset:
						return "RSET";

					case Pop3Command.Retrieve:
						return "RETR";

					case Pop3Command.Status:
						return "STAT";

					case Pop3Command.User:
						return "USER";

					case Pop3Command.Quit:
						return "QUIT";
				}
			}
			return base.ConvertTo( context, culture, value, destinationType );
		}
	}
}
